// 导入数据库
const db = require('../db/index')

// 更新并得到用户的未读列表read_list和read_status 用户id,department =>
exports.getReadList = (req, res) => {
    const { id, department } = req.body
    const sql1 = "select * from message where (message_receiver = ? and message_status = 0) or (message_receiver = '全体部门' and message_status = 0)"// 根据部门找到应该接收到的信息
    db.query(sql1, department, (err, result) => {
        if (err) return res.cc(err)
        const read_list = result.map(e => e.id)// 将传入部门接收消息的id保存在数组read_list中
        const sql2 = "update users set read_list = ?, read_status = 1 where id = ?"
        db.query(sql2, [JSON.stringify(read_list), id], (err, result) => {
            if (err) return res.cc(err)
            res.send(read_list)
        })
    })
}

// 得到消息列表(用户部门消息加全体部门消息) department =>
exports.returnMessageBoxData = (req, res) => {
    const { department } = req.body
    const sql1 = "select * from message where (message_receiver = ? and message_status = 0) or (message_receiver = '全体部门' and message_status = 0) order by message_create_time DESC"
    db.query(sql1, department, (err, result) => {
        if (err) return res.cc(err)
        res.send(result)
    })
}

// 得到单个用户的未读列表及状态 用户id =>
exports.getReadListAndStatus = (req, res) => {
    const { id } = req.body
    const sql1 = "select read_list,read_status from users where id = ?"
    db.query(sql1, id, (err, result) => {
        if (err) return res.cc(err)
        res.send(result[0])
    })
}

// 用户点击消息后，对未读列表read_list里的数据进行删减 用户userId,点击消息messageId =>
exports.clickToRead = (req, res) => {
    const { userId, messageId } = req.body
    const sql1 = "select read_list from users where id = ?"
    db.query(sql1, userId, (err, result) => {
        if (err) return res.cc(err)
        const list = JSON.stringify(JSON.parse(result[0].read_list).filter(item => item != messageId))
        const sql2 = "update users set read_list = ? where id = ?"
        db.query(sql2, [list, userId], (err, result) => {
            if (err) return res.cc(err)
            res.send({
                status: 0,
                message: '阅读列表删减成功'
            })
        })
    })
}

// 把新发布的消息插入到对应部门用户的read_list中 新发布消息messageId,department =>
exports.addReadList = (req, res) => {
    const { messageId, department } = req.body
    if (department === '全体部门') {
        const sql1 = "select read_list,id from users where read_status = 1"
        db.query(sql1, (err, result) => {
            if (err) return res.cc(err)
            const sql2 = "update users set read_list = ? where id = ?"
            let list = []
            result.forEach(e => {
                list = JSON.parse(e.read_list)
                list.push(+messageId)
                db.query(sql2, [JSON.stringify(list), e.id], (err, result) => { if (err) return res.cc(err) })
            })
            res.send({
                status: 0,
                message: '增加系统消息成功'
            })
        })
    } else {
        const sql1 = "select read_list,id from users where department = ? and read_status = 1"
        db.query(sql1, department, (err, result) => {
            if (err) return res.cc(err)
            const sql2 = "update users set read_list = ? where id = ?"
            let list = []
            result.forEach(e => {
                list = JSON.parse(e.read_list)
                list.push(+messageId)
                db.query(sql2, [JSON.stringify(list), e.id], (err, result) => { if (err) return res.cc(err) })
            })
            res.send({
                status: 0,
                message: '增加公司公告成功'
            })
        })
    }
}

// 把新删除的消息从对应部门用户的read_list中删减 新删减消息messageId,department =>
exports.reduceReadList = (req, res) => {
    const { messageId, department } = req.body
    if (department === '全体部门') {
        const sql1 = "select read_list,id from users where read_status = 1"
        db.query(sql1, (err, result) => {
            if (err) return res.cc(err)
            const sql2 = "update users set read_list = ? where id = ?"
            result.forEach(e => {
                db.query(sql2, [JSON.stringify(JSON.parse(e.read_list).filter(item => item != messageId)), e.id], (err, result) => { if (err) return res.cc(err) })
            })
            res.send({
                status: 0,
                message: '删减系统消息成功'
            })
        })
    } else {
        const sql1 = "select read_list,id from users where department = ? and read_status = 1"
        db.query(sql1, department, (err, result) => {
            if (err) return res.cc(err)
            const sql2 = "update users set read_list = ? where id = ?"
            result.forEach(e => {
                db.query(sql2, [JSON.stringify(JSON.parse(e.read_list).filter(item => item != messageId)), e.id], (err, result) => { if (err) return res.cc(err) })
            })
            res.send({
                status: 0,
                message: '删减公司公告成功'
            })
        })
    }
}